<?php
/**
 * DataObjectWeds
 *
 * DataObjectWeds is the extended version of the DataObject class for handling MySQL data.
 * It adds the "user added" and "user edited" info to the data row. Rememeber, the table
 * must have this fields.
 * @author King Beto <apardo@adverit.com>
 * @version 1.0
 * @package database
 */
require_once(dirname(__FILE__) ."/DataObject.php");

/**
 * DataObjectWeds is the extended version of the DataObject class for handling MySQL data.
 * @package database
 */
class DataObjectWed extends DataObject
{
    /**
     * The user name that created the row. This will not be updated on update()
     * @access public
     * @var string
     */
    public $userCreated = '';
    /**
     * The user name that modified the row. This will not be updated on update()
     * @access public
     * @var string
     */
    public $userModified = '';


    // I need
    public function __construct()
    {
        // The date that the row was created (yyyy-mm-dd).
        array_push($this->_arFields, 'date_created');
        // The date that the row was modified (yyyy-mm-dd).
        array_push($this->_arFields, 'userID_created');
        // The userID that created the row.
        array_push($this->_arFields, 'date_modified');
        // The userID that modified the row.
        array_push($this->_arFields, 'userID_modified');
        parent::__construct();
    }

    /**
     * Sets the DataObjectWed fields values from the database following the setted ID.
     * @param integer|string|array $opt
     * @return integer
     */
    public function get($opt)
    {
        $this->_debugLog(2,get_class($this) ."->get($opt)");
        if ($this->_arFields==0) return 0;

        if (is_array($opt))
        {
            $condition = $this->_prepareCondition($opt, true, "t.");
        }
        else if ($opt) $id = $opt;
        else return 0;

        $sql  = "SELECT t.". implode(", t.", $this->_arFields) .", uc.username AS userCreated, um.username AS userModified FROM ". $this->_dbTable ." AS t
                 LEFT JOIN ". $GLOBALS["CONF"]["db"]["prefix"] ."users AS uc ON (t.userID_created=uc.userID)
                 LEFT JOIN ". $GLOBALS["CONF"]["db"]["prefix"] ."users AS um ON (t.userID_modified=um.userID)";
        $sql .= $id? " WHERE t.". $this->_dbIndex ."='". $id ."'" : $condition;
        if ($this->_mysqlQuery($sql, $this->_mysqlQueryResult) && mysql_num_rows($this->_mysqlQueryResult)>0)
        {
            $this->set(mysql_fetch_assoc($this->_mysqlQueryResult));
            return 1;
        }
        return 0;
    }

    /**
     * Set the DataObjectWed fields values. The array must have the fields' name as keys.
     * @param array() $data
     * @param boolean stripslashes
     * @return void
     */
    public function set($data, $stripslashes=false)
    {
        parent::set($data, $stripslashes);
        $this->userCreated  = $data['userCreated']? $data['userCreated'] : $this->userCreated;
        $this->userModified = $data['userModified']? $data['userModified'] : $this->userModified;
    }

    /**
     * Creates a DataObjectWed row following the internal data setted with set() or get().
     * @return integer
     */
    public function create()
    {
        $this->_debugLog(2, get_class($this) ."->create()<br>\n");
        if (!$this->_checkDBLink()) return 0;

        $this->date_created = date('Y-m-d H:i:s');
        $this->userID_created = $this->userID_modified;

        $sql = "INSERT INTO ". $this->_dbTable ."( ";

        foreach($this->_arFields AS $field)
        {
            if ($field == "date_modified") continue;
            $sql .= "`". $field ."`,";
        }

        $sql = substr($sql, 0, -1) ." ) VALUES (";

        foreach($this->_arFields AS $field)
        {
            if ($field == "date_modified") continue;

            if(is_null($this->$field)) $sql .= "NULL,";
            else $sql .= "'". $this->txt2sql($this->$field) ."',";
        }

        $sql = substr($sql, 0, -1) ." )";
        $this->_debugLog(3, get_class($this) .$sql."<br>\n");
        if ($this->_mysqlQuery($sql))
        {
            $this->setID($this->getInsertedID());
            return $this->getID();
        }
        return 0;
    }

    protected function getInsertedID()
    {
        return mysql_insert_id($this->_dbLink);
    }

    /**
     * Updates the DataObjectWed row following the internal data setted with set() or get().
     * @param integer|string $id
     * @return integer
     */
    public function update($id=null)
    {
        $this->_debugLog(2, get_class($this) ."->update()<br>\n");
        if (!$this->_checkDBLink()) return 0;

        $id = $id? $id : $this->getID();
        $sql = "UPDATE ". $this->_dbTable ." SET ";
        foreach($this->_arFields AS $field)
        {
            if ($field == "date_modified") continue;
            $sql .= "`". $field ."`='". $this->txt2sql($this->$field) ."', ";
        }
        $sql = substr($sql, 0, -2) ." WHERE ". $this->_dbIndex ."='". $this->getID() ."'";
        return $this->_mysqlQuery($sql);
    }
}

?>
